﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Windows;

namespace Xceed.Silverlight.ListBox
{
  public abstract class ListBoxContainerConfiguration<T> : ListBoxContainerConfigurationBase
    where T : ListBoxContainer
  {
    #region CONSTRUCTORS

    protected ListBoxContainerConfiguration()
      : base()
    {
    }

    protected ListBoxContainerConfiguration( ListBoxContainerConfiguration<T> template )
      : base( template )
    {
    }

    #endregion CONSTRUCTORS

    #region PROTECTED METHODS

    protected abstract T CreateContainerCore();

    protected virtual void PrepareContainerForItemCore( T container, object item )
    {
      // Apply the desired ContainerLayoutMode if different from ContainerLayoutMode.Default.
      ContainerLayoutMode configurationLayoutMode = this.LayoutMode;

      if( configurationLayoutMode != ContainerLayoutMode.Default )
      {
        // This ContainerLayoutMode is not used to position the container within the Panel/Active View.
        // It is only used to drive the container's visual states in order to offer a way of changing
        // the container's template dynamically.
        // It is pre-emptively set in the ListBox's PrepareContainerForItem method to the one used
        // to position the container by the Panel/ActiveView.
        ListBoxContainer.SetLayoutMode( container, configurationLayoutMode );
      }

      // Apply the container style.
      Style itemContainerStyle = this.ContainerStyle;

      if( itemContainerStyle != null )
      {
        container.Style = itemContainerStyle;
      }
      else
      {
        container.ClearValue( ListBoxContainer.StyleProperty );
      }

      // Apply the container content template.
      DataTemplate itemTemplate = this.ContentTemplate;

      if( itemTemplate != null )
      {
        container.ContentTemplate = itemTemplate;
      }
      else
      {
        // Clear the value so that the default object's DataTemplate is used.
        container.ClearValue( ListBoxContainer.ContentTemplateProperty );
      }
    }

    #endregion PROTECTED METHODS

    #region INTERNAL METHODS

    internal override ListBoxContainer CreateContainer()
    {
      return this.CreateContainerCore();
    }

    internal override void PrepareContainerForItem( ListBoxContainer container, object item )
    {
      // Set on the container a reference to the configuration that was used to prepare it.
      container.Configuration = this;

      this.PrepareContainerForItemCore( container as T, item );
    }

    #endregion INTERNAL METHODS
  }
}
